<?php
class ReviewModel extends CI_Model
{
    // Mongo
    const collection = 'review';

    // Cache
    const cache_enable = true;
    const cache_ttl = 86400;
    const cache_list_ttl = 900;
    const prefix_cache_item = 'review.%s';
    const prefix_cache_list = 'review.list.%s.%s.%s.%';
    const prefix_cache_count = 'review.count.%s.%s';


    public function  __construct()
    {
        parent::__construct();
    }

    public function initData($data)
    {
        // Check $data
        if(!is_array($data)) return null;

        // Define fields
        $fields = array('_id', 'user_id', 'company_id', 'content', 'created_date');

        $list = null;

        foreach($fields as $key) {
            if(isset($data[$key])) {
                $list[$key] = $data[$key];
            }
        }

        // Return
        return $list;
    }

    public function insert($data)
    {
        // Init data
        $data = $this->initData($data);

        // Check
        if(empty($data) || !isset($data['user_id']) || !isset($data['company_id'])) return false;

        // Insert to DB
        $rs = $this->mongo_db->insert(self::collection, $data);

        return $rs;
    }

    public function update($data)
    {
        // Init data
        $data = $this->initData($data);

        // Check
        if(empty ($data) || !isset($data['_id'])) return false;

        $rs = $this->mongo_db->update(self::collection, $data);

        // Check
        if($rs) {

            // Delete cache
            $this->cache->memcached->delete(sprintf(self::prefix_cache_item, $data['_id']));
        }

        // Return
        return $rs;
    }

    public function delete($id)
    {
        // Check
        if(empty($id)) return false;

        // Delete
        $rs = $this->mongo_db->where(array('_id' => $id))->delete(self::collection);
        
        // Return
        return $rs;
    }

    public function selectReview($id)
    {
        // Check
        if(empty($id)) return false;

        // Init
        $data = false;
        
        // Cache key
        $cacheKey = sprintf(self::prefix_cache_item, $id);

        // Read cache
        if(self::cache_enable) {
            $data = $this->cache->memcached->get($cacheKey);
        }

        // Check
        if($data === false) {

            // Select from DB
            $data = $this->mongo_db->where(array('_id' => $id))->findOne(self::collection);

            // Write to cache
            $this->cache->memcached->save($cacheKey, $data, 0, self::cache_ttl);
        }

        // Return
        return $data;
    }

    public function selectReviewByUser($userId, $page)
    {
        $list = false;
        
        // Check
        if(empty($userId) || empty($page)) return $list;

        // Create cache key
        $cacheKey = sprintf(self::prefix_cache_list, 'user_id', $userId, $page, FE_REVIEW_LIMIT);

        // Read cache
        if(self::cache_enable) {
            $list = $this->cache->memcached->get($cacheKey);
        }

        // Check
        if($list === false) {
            
            // Set offset
            $offset = ($page - 1) * FE_REVIEW_LIMIT;

            // Select
            $list = $this->mongo_db->where(array('user_id' => $userId))->offset($offset)->limit(FE_REVIEW_LIMIT)->get(self::collection);

            // Write to cache
            $this->cache->memcached->save($cacheKey, $list, 0, self::cache_list_ttl);
        }
        // Return
        return $list;
    }

    public function selectReviewByCompany($companyId, $page)
    {
        $list = false;

        // Check
        if(empty($companyId) || empty($page)) return $list;

        // Create cache key
        $cacheKey = sprintf(self::prefix_cache_list, 'company_id', $companyId, $page, FE_PRODUCT_LIMIT);

        // Read cache
        if(self::cache_enable) {
            $list = $this->cache->memcached->get($cacheKey);
        }

        if($list === false) {

            // Set offset
            $offset = ($page - 1) * FE_REVIEW_LIMIT;

            // Select
            $list = $this->mongo_db->where(array('company_id' => $companyId))->offset($offset)->limit(FE_REVIEW_LIMIT)->get(self::collection);

            // Write to cache
            $this->cache->memcached->save($cacheKey, $list, 0, self::cache_list_ttl);
        }
        
        // Return
        return $list;
    }

    public function selectTotalReview($id, $type)
    {
        // Check
        if(empty($id)) return 0;

        // Init data
        $total = 0;

        if($type ==  1) {
            $field = 'company_id';
        } else {
            $field = 'user_id';
        }

        // Create cache key
        $cacheKey = sprintf(self::prefix_cache_count, $field, $id);

        // Read cache
        if(self::cache_enable) {
            $total = $this->cache->memcached->get($cacheKey);
        }

        // Check
        if($total === false) {

            // Select
            $total = $this->mongo_db->where(array($field => $id))->count();

            // Write to cache
            $this->cache->memcached->save($cacheKey, $total, 0, self::cache_ttl);
        }
        // Return
        return $total;
    }   
}
?>